<?php
include("../utility/connection.php");
include("../model/mUser.php");
include("../model/mUserSeminar.php");
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST") {
    $ubah = trim(mysql_real_escape_string($_POST["hidUbah"]));
    if ($ubah == '1') {
        // ambil detil untuk ditampilakn di kolom perubahan
        $UserId = trim(mysql_real_escape_string($_POST["hidIdUser"]));
        $result = fnSelectUser($UserId);
        $count = mysql_num_rows($result); // number of row
        $ubahUser = mysql_fetch_array($result);

        if ($count == 1) {
            $_SESSION['userId']=$UserId;
            $_SESSION['ubah']=$ubah;
            $_SESSION['ubahUser']=$ubahUser;
            header("location: ../ui/admin/userAdmin.php");
        } else {
            $_SESSION['userId']=$UserId;
            unset($_SESSION['ubah']);
            unset($_SESSION['ubahUser']);
            header("location: ../ui/admin/userAdmin.php");
        }
    } else if ($ubah == '2') {
        // hapus session2 yg gak diperlukan
        unset($_SESSION['ubah']);
        unset($_SESSION['ubahUser']);
        // simpan perubahan
        $UserId = trim(mysql_real_escape_string($_POST["hidIdUser"]));
        $Nama = trim(mysql_real_escape_string($_POST["txtNama"]));
        $Alamat = trim(mysql_real_escape_string($_POST["txtAlamat"]));
        $Kota = trim(mysql_real_escape_string($_POST["txtKota"]));
        $Propinsi = trim(mysql_real_escape_string($_POST["txtPropinsi"]));
        $Negara = trim(mysql_real_escape_string($_POST["txtNegara"]));
        $KodePos = trim(mysql_real_escape_string($_POST["txtKodePos"]));
        $Telp = trim(mysql_real_escape_string($_POST["txtTelp"]));
        $Email = trim(mysql_real_escape_string($_POST["txtEmail"]));
        $LoginId = trim(mysql_real_escape_string($_POST["txtLoginId"]));
        $Password = trim(mysql_real_escape_string($_POST["txtPassword"]));
        fnUpdateUser($Nama,$Alamat,$Kota,$Propinsi,$Negara,$KodePos,$Telp,$Email,$LoginId,$Password,$UserId);
        // simpan status user di masing2 seminar
        $JmlSeminar = trim(mysql_real_escape_string($_POST["hidJmlSeminar"]));
//        echo "jml seminar: ".$JmlSeminar."<br>";
        if ($JmlSeminar > 0) {
            for ($a=1; $a<=$JmlSeminar; $a++) {
                $SeminarId = trim(mysql_real_escape_string($_POST["hidIdSeminar_".$a]));
                $UserGroup = trim(mysql_real_escape_string($_POST["hidUserGroup_".$a]));
//                echo "user id: ".$UserId.", seminar id: ".$SeminarId.", user group: ".$UserGroup."<br>";
                // cek terlebih dahulu, apakah user tsb telah terdaftar pada tabel utk seminar yg dimaksud
                // bila sudah, update
                // bila belum, insert
                $cek = fnSelectUserSeminar($SeminarId,$UserId);
                $countCek = mysql_num_rows($cek);
                if ($countCek > 0) {
                    fnUpdateUserSeminar($SeminarId,$UserId,$UserGroup);
                } else {
                    fnInsertUserSeminar($SeminarId,$UserId,$UserGroup);
                }
            }
        }
        // ambil detil lagi
        $result = fnSelectUser($UserId);
        $count = mysql_num_rows($result); // number of row
        $userResult = mysql_fetch_array($result);

        if ($count == 1) {
            $_SESSION['userId']=$UserId;
            $_SESSION['userResult']=$userResult;
            unset($_SESSION['slctUser']);
            header("location: ../ui/admin/userAdmin.php");
        } else {
            $_SESSION['userId']=$UserId;
            unset($_SESSION['userResult']);
            unset($_SESSION['slctUser']);
            header("location: ../ui/admin/userAdmin.php");
        }
    }
}

?>
